<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;

class RootUser extends Controller{
    public function showRootUser(){
        $data=Db::name("root_user")->select();
        return ['status'=>1,'message'=>'获取成功','data'=>$data];
    }

    public function insert(){
        $data=input();
        $data['root_user_password']=md5($data['root_user_password']);
        if(Db::name("root_user")->insert($data)){
            $root_user_id=Db::name("root_user")->getLastInsID();
            return ['status'=>1,'message'=>'添加成功','root_user_id'=>$root_user_id];
        }
        else
            return ["status"=>0,'message'=>'添加失败'];
    }

    public function login(){
        $root_user_username=input('root_user_username');
        $root_user_password=md5(input('root_user_password'));
        $data=Db::name("root_user")->where('root_user_username',$root_user_username)->value('root_user_password');
        if($root_user_password==$data)
            return ['status'=>1,'message'=>'登录成功','root_user_username'=>$root_user_username];
        else
            return ['status'=>0,'message'=>'登陆失败'];
    }

    public function update(){
        $data=input();
        if($data['column']=="root_user_password"){
            $data['value']=md5($data['value']);
        }
        if(Db::name("root_user")->where("root_user_id",$data['id'])->setField($data['column'],$data['value']))
            return ['status'=>1,'message'=>'更新成功'];
        else
            return ['status'=>0,'message'=>'更新失败'];
    }

    public function delete(){
        $root_user_id=input("id");
        if(Db::name("root_user")->where("root_user_id",$root_user_id)->delete())
            return ['status'=>1,'message'=>'删除成功'];
        else
            return ['status'=>0,'message'=>'删除失败'];
    }

    public function getone(){
        $root_user_id=input("id");
        $data=Db::name("root_user")->where('root_user_id',$root_user_id)->find();
        return ['status'=>1,'message'=>'获取成功','data'=>$data];
    }
}